package descentFly.away3D4.element
{
	import away3d.containers.ObjectContainer3D;
	
	import descentFly.away3D4.df_awaySpace;
	import descentFly.data.df_fields;
	import descentFly.df_spaceObject;
	
	import flash.events.TimerEvent;
	import flash.geom.Matrix3D;
	import flash.utils.Timer;
	
	import space.sp_utils;
	
	public class df_rollElem extends df_element3DS {
		public static const FIELD_ROLL: String = 'roll';
		
		public static const DEFAULT_FPS: Number = 16;
		public static const DEFAULT_ROLL: Array = [0, 8, 0];
		
		private var _timer		: Timer;
		
		public function df_rollElem(mat: Matrix3D, a_space:df_awaySpace, a_options:Object) {
			sp_utils.checkParam(a_options, FIELD_ROLL, DEFAULT_ROLL);
			sp_utils.checkParam(a_options, df_fields.FIELD_FPS, DEFAULT_FPS);
			if (a_options.roll is String) {
				a_options.roll = a_options.roll.split(',');
				a_options.roll[0] = parseFloat(a_options.roll[0]);
				a_options.roll[1] = parseFloat(a_options.roll[1]);
				a_options.roll[2] = parseFloat(a_options.roll[2]);
			}
			super(mat, a_space, a_options);
		}
		
		override protected function setModel(a_model: ObjectContainer3D): void {
			super.setModel(a_model);
			
			_timer = df_spaceObject.createTimer(1000 / options.fps, doTimer);
			_timer.start();
		}
		
		protected function doTimer(e: TimerEvent): void {
			var model: ObjectContainer3D = getModel();
			model.rotationX += options.roll[0];
			model.rotationY += options.roll[1];
			model.rotationZ += options.roll[2];
		}
		
		override public function dispose():void {
			if (_timer) _timer.removeEventListener(TimerEvent.TIMER, doTimer);
			super.dispose();
		}
	}
}